package com.admin.exam.service;

import java.util.*;

import com.admin.exam.entity.HospitalRank;
import com.admin.exam.entity.PersonInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.base.Page;
import com.admin.exam.dao.HospitalMapper;
import com.admin.exam.entity.Hospital;
import com.admin.exam.vo.query.HospitalQuery;

/**
 * 操作相关
 */
@Service
public class HospitalService {
    @Autowired
    private HospitalMapper hospitalMapper;

    public Hospital getById(Integer id) {
        return hospitalMapper.getByPrimaryKey(id);
    }

    public void add(Hospital record) {
        hospitalMapper.insertSelective(record);
    }

    /**
     * 批量删除
     */
    public void delete(Integer[] ids) {
        for (Integer id : ids) {
            hospitalMapper.deleteByPrimaryKey(id);
        }
    }

    public void update(Hospital record) {
        hospitalMapper.updateByPrimaryKeySelective(record);
    }

    public List<Hospital> listByConditions(HospitalQuery query) {
        return hospitalMapper.listByConditions(query);
    }

    public List<HospitalRank> getHospitalRank(List<PersonInfo> personalRank) {
        Map<Integer, HospitalRank> hospitalMap = new HashMap<>();
        //统计每个医院前三成绩
        for (int i = 0; i < personalRank.size(); i++) {
            PersonInfo personInfo = personalRank.get(i);
            HospitalRank hospital = hospitalMap.get(personInfo.getHospital());
            if (Objects.isNull(hospital)) {
                hospital = new HospitalRank();
                hospital.setHospital(personInfo.getHospital());
                hospital.setHospitalName(personInfo.getHospitalName());
                hospital.setScores(personInfo.getScores());
                hospital.setTime(personInfo.getTime());
                hospital.setCount(1);
            } else {
                if (hospital.getCount() < 3) {
                    hospital.setScores(hospital.getScores() + personInfo.getScores());
                    hospital.setTime(hospital.getTime() + personInfo.getTime());
                    hospital.setCount(hospital.getCount() + 1);
                }
            }
            hospitalMap.put(hospital.getHospital(), hospital);
        }
        List<HospitalRank> hospitalRank = new ArrayList<>(hospitalMap.values());

        Collections.sort(hospitalRank, (o1, o2) -> {
            if (o2.getScores() - o1.getScores() == 0) {
                return o1.getTime() - o2.getTime();
            } else {
                return o2.getScores() - o1.getScores();
            }
        });

        for (int i = 0; i < hospitalRank.size(); i++) {
            HospitalRank hospital = hospitalRank.get(i);
            hospital.setRank(i + 1);
        }
        return hospitalRank;
    }

    public Long pageCount(HospitalQuery query) {
        return hospitalMapper.pageCount(query);
    }

    public Page<Hospital> pagingByConditions(HospitalQuery query) {
        return hospitalMapper.pagingByConditions(query);
    }
}

